package org.yatr.ttcn.rt.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.etsi.ttcn.tci.QualifiedName;
import org.etsi.ttcn.tci.Value;
import org.yatr.tci.IntegerValueYatr;

public class CallFunction {
	private final QualifiedName qualifiedName;
	private Method method;
	private Class clazz;
	public CallFunction(Class<?> clazz, String moduleName, String funcName) {
		qualifiedName= TypeFactory.newQualifiedName(moduleName, funcName);
		this.clazz = clazz;
		try {
			
			if(funcName.equals("control")){
				method = clazz.getDeclaredMethod(funcName);
			} else {
				method = clazz.getDeclaredMethod(funcName, Component.class, Component.class, Component.class, Value[].class);
			}
			method.setAccessible(true);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	public void run(Component self, Component mtc, Component sys, Value... args){
		try {
			if(method.getName().equals("control")){
				method.invoke(clazz);
			} else {
				method.invoke(clazz, self, mtc, sys, args);
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	public QualifiedName getQualifiedName(){
		return qualifiedName;
	}
	
}
